﻿using System;

class MaxSumSequence
{
    static void Main()
    {
        //Exercise 8.
        //Write a program that finds the sequence of maximal sum in given array. Example:
        // {2, 3, -6, -1, 2, -1, 6, 4, -8, 8} -> {2, -1, 6, 4}
        //Can you do it with only one loop (with single scan through the elements of the array)?

        
        //enter the length and the elements of the array from the console
        Console.Write("Type the length of the array (N):  ");
        int N = int.Parse(Console.ReadLine());
        Console.WriteLine("Type the elements of the array:");
        int[] array = new int[N];
        for (int i = 0; i < N; i++)
        {
            array[i] = int.Parse(Console.ReadLine());
        }

        int sum = 0;
        int bestSum = int.MinValue;
        int index = -1;
        int count = 0;
        int bestCount = 0;
        int firstIndexofSeq = -1;
        for (int i = 0; i < array.Length; i++)
        {
            count = 0;
            sum = 0;
            for (int j = i; j < array.Length; j++)
            {
                sum = sum + array[j];
                count++;
                if (sum > bestSum)
                {
                    bestSum = sum;
                    index = j;                   
                    bestCount = count;
                    firstIndexofSeq = index - bestCount + 1;
                }
            }                      
        }
        
        Console.WriteLine();
        for (int i = 0; i < bestCount; i++)
        {
            Console.Write(array[firstIndexofSeq + i] + " ");
        }
        Console.WriteLine();
    }
}

